package work.timejay.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import work.timejay.common.SuperServiceImpl;
import work.timejay.shop.entity.MqLog;
import work.timejay.shop.entity.UserItem;
import work.timejay.shop.mapper.UserItemMapper;
import work.timejay.shop.service.MqLogService;
import work.timejay.shop.service.UserItemService;
import org.springframework.stereotype.Service;
import work.timejay.shop.vo.UserSend;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wwj
 * @since 2021-12-18
 */
@Service
public class UserItemServiceImpl extends SuperServiceImpl<UserItemMapper, UserItem> implements UserItemService {
    @Autowired
    private MqLogService mqLogService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void itemRecordAdd(UserSend userSend) {
        //幂等性
        MqLog one = mqLogService.lambdaQuery().eq(MqLog::getTxNo, userSend.getTxNo()).one();
        if(null != one){
            return;
        }
        UserItem userItem = lambdaQuery().eq(UserItem::getUserId, userSend.getIncomeId()).one();
        userItem.setCount(userItem.getCount()+1);
        updateById(userItem);
        if (true) {
            throw new RuntimeException("人为异常");
        }
    }
}
